<?php
namespace app\admin\model;
use think\Model;
use think\Db;

/**
 * 后台渠道key模型
 * @package app\admin\model
 */
class Channelip extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'bus_channel_ip';
    protected $observerClass = 'app\admin\event\DeleteCache';
    public function searchOffRedisAttr($query, $value, $data)
    {
        if(isMmsOrSms()){
            $query->where('off_redis','=', 0);
        }
        
    }
    public function getIp()
    {
        return $this::order('channel_id asc')->select()->toArray();
    }

    //ip开关
    public function openOrClose($id,$status)
    {
        trace(session('username').'把白名单id为:'.$id.'的开启状态改为了'.$status,'info');
        return $this::isUpdate(true)->save(['ip_status' => $status],['id'=>$id]);
    }

    //获取主页信息
    public function getAllChannel()
    {
        if(is_OneCha()){
            $where['c.p_id'] = session('relate_id');
        }else{
            $where = [];
        }
        return $this::alias('i')->join('bus_channel c','i.channel_id = c.channel_id')->field('i.channel_id,i.id,i.channel_ip,i.ip_status,c.company_name')->where($where)->order('channel_id asc')->paginate(20);
    }

    //获取当前ip
    public function getOne($id)
    {
        $data['id'] = $id;
        return $this::get($data)->toArray();
    }

    //更新数据
    public function upd($data)
    {
        trace(session('username').'更新白名单id为:'.$data['id'].'的数据为了'.json_encode($data, JSON_UNESCAPED_UNICODE),'info');
        return $this::isUpdate(true)->allowField(true)->save($data,['id' => $data['id']]);
    }

    //条件查询
    public function getChannel($data)
    {
        if($data == 0){
            return $this::alias('i')->join('bus_channel c','i.channel_id = c.channel_id')->field('i.channel_id,i.id,i.channel_ip,i.ip_status,c.company_name')->order('channel_id asc')->paginate(20);
        }else{
            $where['i.channel_id'] = $data;
            return $this::alias('i')->join('bus_channel c','i.channel_id = c.channel_id')->field('i.channel_id,i.id,i.channel_ip,i.ip_status,c.company_name')->where($where)->paginate(20);
        }
    }

    /**
     * [getChannels 条件查询channel_ip]
     * @author   .guo
     * @DataTime 2019-05-28
     * @param    [array]     $request [条件]
     * @return   [type]               [description]
     */
    public function getChannels($request)
    {
        $where = [];

        if (isset($request['cha']) && !empty($request['cha'])) {

            $where['i.channel_id'] = $request['cha'];
        }

        if (session('user_type') == 'channel') {

            $where['i.channel_id'] = session('relate_id');
        }

        return $this::withSearch(['off_redis'])->alias('i')
            ->join('bus_channel c','i.channel_id = c.channel_id')
            ->field('i.channel_id,i.id,i.channel_ip,i.client_ip,i.ip_status,c.company_name,c.channel_type')
            ->where($where)->paginate($request['limit'], false, $request)
            ->each(function($item, $key) {

                switch ($item->ip_status) {

                    case '1':
                        $item->ip_status_ = '<span class="layui-badge layui-bg-green status-qy">已启用</span>';
                        break;

                    case '0':
                        $item->ip_status_ = '<span class="layui-badge status-jy">已禁用</span>';
                        break;

                    default:
                        $item->ip_status_ = '';
                        break;
                }
            });
    }

    //获取主页拉卡框的信息
    public function getMenu()
    {
        if(is_OneCha()){
            $where['p_id'] = session('relate_id');
        }elseif(session('user_type') == "admin"){
            $where = [];
        }else{
            return [];
        }
        return Channel::withSearch(['off_redis'])->field('channel_id,company_name')->where($where)->group('channel_id')->select();
        // return $this::alias('i')->join('bus_channel c','i.channel_id=c.channel_id','right')->field('i.channel_id,c.company_name')->where($where)->group('i.channel_id')->select()->toArray();
    }

    //添加一条数据
    public function addOne($data)
    {
        return $this->save($data);
    }
}
